$(function () {
    // 去注册
    $('#link_reg').on('click', function (e) {
        $('.login_Box').hide();
        $('.register_Box').show();
    })
    // 去登录
    $('#link_login').on('click', function (e) {
        $('.login_Box').show();
        $('.register_Box').hide();
    })
    // --------------------------------
    var form = layui.form;
    form.verify({
        pwd: [/^[\S]{6,12}$/, '密码必须6到12位，且不能出现空格'],
        repwd: function (value) {
            var pwd = $('.register_Box [name=password]').val();
            if (pwd != value) {
                return '两次密码不一致！'
            }
        },
        role: [/S/, "请选择角色"],
        sno: [/^[\S]{9}$/, '学号或工号必须9位，且不能出现空格']
    });

    //----------------------------------------------------------------
    // 注册
    var layer = layui.layer;
    $('#form_reg').on('submit', function (e) {
        // 阻止默认提交事件
        e.preventDefault();
        var access_name = null;
        var access = $('#form_reg [name = access]:checked').val()
        if (access == 0) {
            access_name = "学生"
        } else if (access == 2) {
            access_name = "老师"
        } else {
            access_name = "无权限"
        }
        var data = {
            username: $('#form_reg [name = username]').val(),
            sno: $('#form_reg [name =sno]').val(),
            college: $('#form_reg [name = college]').val(),
            aclass: $('#form_reg [name = aclass]').val(),
            phone_number: $('#form_reg [name = phone_number]').val(),
            password: $('#form_reg [name = password]').val(),
            access_name: access_name,
        }
        const table_data = $(this).serialize();
        var searchParams = new URLSearchParams(table_data);
        var access_val = searchParams.get("access");
        if (access_val == undefined) {
            layer.msg('请选择角色学生或老师!');
        } else {
            $.post('/api/reguser', data, function (res) {
                if (res.status !== 0) {
                    // return console.log(res.message);
                    return layer.msg(res.message);
                }
                // console.log('注册成功！');
                layer.msg('注册成功！！');
                // 模拟人的点击行为
                $('#link_login').click();
            })
        }
    })

    // 登录
    $('#form_login').on('submit', function (e) {
        // 阻止默认提交行为
        e.preventDefault();
        // 获取用户提交数据
        const data = $(this).serialize();
        var searchParams = new URLSearchParams(data);
        // 筛选access变量的值
        var access_val = searchParams.get("access");
        $.ajax({
            method: 'POST',
            url: '/api/login',
            data: data,
            success: function (res) {
                if (res.status == 1) {
                    return layer.msg('登录失败！请确认密码是否正确');
                }
                if (res.status == 3) {
                    return layer.msg('登录失败！请检查登录类型', {
                        icon: 4,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    });
                }
                layer.msg('登录成功');
                console.log(res.token);
                // 将登录成功得到的 token 字符串，保存到 localStorage 中
                localStorage.setItem('token', res.token);
                // 跳转到后台主页
                if (access_val == 0) {
                    return location.href = './page/student.html';
                }
                if (access_val == 2) {
                    return location.href = './page/teacher.html';
                }
                if (access_val == 3) {
                    return location.href = './page/admin.html';
                }
            }
        })
    })
})